<template>
  <el-form-item 
    :label="subschema.title"
    :prop="field"
  >
    <el-switch
      v-model="model"
      v-bind="props"
    >
    </el-switch>
  </el-form-item>
</template>
<script>
import { hasOwnPro } from '../../utils';
export default {
  name: 'ElFormilySwitch',
  props: {
    subschema: {
      type: Object,
      required: true
    },
    field: {
      type: String,
      required: true
    }
  },
  inject: ['formCoreRef', 'formRef'],
  data() {
    return {
      props: this.subschema.props,
      // ons: eventsKebabCase(pick(this.subschema, (prop) => eventReg.test(prop))),
      formData: this.formCoreRef.formData
    }
  },
  created() {
    new Proxy(this.props, {
      get(target, prop) {
        console.log('get', prop)
        return target[prop];
      }
    })
  },
  computed: {
    model: {
      get() {
        if(hasOwnPro(this.formData, this.field)) {
          return this.formData[this.field];
        }
        if(hasOwnPro(this.subschema, 'default')) {
          this.$nextTick(() => {
            this.model = this.subschema.default;
          })
        }
      },
      set(val) {
        this.$set(this.formData, this.field, val);
      }
    }
  }
}
</script>